package de.gwtprime.client.widgets.trading.popup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.OrderProcessedEvent;
import de.gwtprime.client.widgets.trading.OrderOverviewWidget;
import de.gwtprime.shared.requestFactory.OrderProxy;

public class TradeConfirmationPopup extends PopupPanel  {

	private static TradeConfirmationPopup2UiBinder uiBinder = GWT
			.create(TradeConfirmationPopup2UiBinder.class);

	interface TradeConfirmationPopup2UiBinder extends
			UiBinder<Widget, TradeConfirmationPopup> {
	}

	
	@UiField Button confirm;
	@UiField Button cancel;
	@UiField SimplePanel orderOverview;
	
	public TradeConfirmationPopup(final ClientFactory clientFactory, final OrderProxy order) {
		super(true, true); // auto hide and modal
		setWidget(uiBinder.createAndBindUi(this));
		 setGlassEnabled(true);
		 
		orderOverview.add(new OrderOverviewWidget(clientFactory, order));
		confirm.setText("Bestätigen");
		
		confirm.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				clientFactory.getRequestFactory().orderRequest().processOrder().using(order).fire(new Receiver<Void>(
						
						) {

							@Override
							public void onSuccess(Void response) {
								
								clientFactory.getEventBus().fireEvent(new OrderProcessedEvent(order));
								clientFactory.getNotificationCenter().showMessage("Kauf abgeschlossen");
								TradeConfirmationPopup.this.hide();
							}
				});
				
			}
		});
		
		cancel.setText("Abbrechen");
		
		cancel.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				TradeConfirmationPopup.this.hide();
				
			}
		});
	}


}
